<?php
namespace Admin\Controller;

class MenuController extends CommonController
{

    /**
     * 列表
     *
     * @param  void
     * @return void
     */
    public function index()
    {
        if (IS_POST) {
            $this->delete();
        } else {
            $get = I('get.');
            $where = array();

            array('between','1,8');

            if (! empty($get['start_date'])) {
                $where['menu_date'] = array( 'egt' , strtotime($get['start_date']) );
            }
            if (! empty($get['end_date'])) {
                $where['menu_date'] = array( 'elt' , strtotime($get['end_date']) );
            }
            if (! empty($get['end_date']) && ! empty($get['start_date'])){
                $where['menu_date'] = array( 'between' , [strtotime($get['start_date']), strtotime($get['end_date'])] );
            }
            if (! empty($get['area_id'])) {
                $where['area_id'] = $get['area_id'];
            }
            $where['status'] = 1;
            $list = $this->lists(M('Menu'), true, $where, 'menu_id DESC', 10);
            $area = getArea(false);
            $this->assign('list', $list);
            $this->assign('area', $area);
            $this->assign('get', $get);
            $this->display();
        }
    }
    
    /**
     * 新增
     *
     * @param  void
     * @return void
     */
    public function add()
    {
        $goods = getGoodsList(false);
        if (IS_POST) {
            $data = I('post.');
            $data['menu_date'] = strtotime($data['menu_date']);
            $model = D('Menu');
            $result = $model->update($data);
            if (false !== $result) {
                $this->success('保存成功', U('index'));
            } else {
                $error = $model->getError();
                $this->error(empty($error) ? '未知错误' : $error);
            }
        } else {
            $area = getArea(false);
            $this->assign('area', $area);
            $this->display();
        }
    }

    /**
     * 修改
     *
     * @param void
     * @return void
     */
    public function edit()
    {
        if (IS_POST) {
            $data = I('post.');
            $data['menu_date'] = strtotime($data['menu_date']);
            $model = D('Menu');
            $result = $model->update($data);
            if (false !== $result) {
                $this->success('保存成功', U('index'));
            } else {
                $error = $model->getError();
                $this->error(empty($error) ? '未知错误' : $error);
            }
        } else {
            $id = I('get.id');
            if (empty($id)) {
                $this->error('无效的数据');
            }
            $info = M('Menu')->where('menu_id = ' . $id)->find();
            $area = getArea(false);
            $this->assign('area', $area);
            $this->assign('info', $info);
            $this->display();
        }
    }

    /**
     * 删除
     *
     * @param void
     * @return void
     */
    public function delete()
    {
        $id = array_unique((array) I('id', 0));
        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $where = array( 'menu_id' => array( 'in', $id ) );
        $data = array( 'status' => 0, 'update_time' => time() );
        $result = M('Menu')->where($where)->save($data);
        if ($result) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }  
    
}
